package com.yserp.project.erp.order.purchorder.controller;

import com.yserp.common.utils.poi.ExcelUtil;
import com.yserp.framework.aspectj.lang.annotation.Log;
import com.yserp.framework.aspectj.lang.enums.BusinessType;
import com.yserp.framework.web.controller.BaseController;
import com.yserp.framework.web.domain.AjaxResult;
import com.yserp.framework.web.page.TableDataInfo;
import com.yserp.project.erp.common.enums.OrderEnums;
import com.yserp.project.erp.order.purchorder.domain.PurchOrder;
import com.yserp.project.erp.order.purchorder.domain.PurchOrderDto;
import com.yserp.project.erp.order.purchorder.domain.PurchOrderVo;
import com.yserp.project.erp.order.purchorder.service.IPurchOrderService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 采购订单Controller
 *
 * @author yserp
 * @date 2022-08-02
 */
@Controller
@RequestMapping("/order/purchorder")
public class PurchOrderController extends BaseController {
    private String prefix = "erp/order/purchorder";

    @Autowired
    private IPurchOrderService purchOrderService;

    @RequiresPermissions("order:purchorder:view")
    @GetMapping()
    public String purchorder() {
        return prefix + "/purchorder";
    }

    /**
     * 查询采购订单列表
     */
    @RequiresPermissions("order:purchorder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PurchOrder purchOrder) {
        startPage();
        List<PurchOrder> list = purchOrderService.selectPurchOrderList(purchOrder);
        return getDataTable(list);
    }

    /**
     * 导出采购订单列表
     */
    @RequiresPermissions("order:purchorder:export")
    @Log(title = "采购订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PurchOrder purchOrder) {
        List<PurchOrder> list = purchOrderService.selectPurchOrderList(purchOrder);
        ExcelUtil<PurchOrder> util = new ExcelUtil<PurchOrder>(PurchOrder.class);
        return util.exportExcel(list, "采购订单数据");
    }

    /**
     * 获取单号
     */
    @GetMapping("/getOrderNo")
    @ResponseBody
    public AjaxResult getOrderNo() {
        return AjaxResult.success("",OrderEnums.getCacheOrderNo(OrderEnums.PURCH_ORDER.getPref()));
    }

    /**
     * 新增采购订单
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("purchOrderNo", OrderEnums.getCacheOrderNo(OrderEnums.PURCH_ORDER.getPref()));
        return prefix + "/add";
    }

    /**
     * 新增保存采购订单
     */
    @RequiresPermissions("order:purchorder:add")
    @Log(title = "采购订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PurchOrderDto purchOrder) {
        return toAjax(purchOrderService.insertPurchOrder(purchOrder));
    }

    /**
     * 修改采购订单
     */
    @RequiresPermissions("order:purchorder:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        PurchOrderVo purchOrder = purchOrderService.selectPurchOrderById(id);
        mmap.put("purchOrderVo", purchOrder);
        return prefix + "/edit";
    }

    /**
     * 修改保存采购订单
     */
    @RequiresPermissions("order:purchorder:edit")
    @Log(title = "采购订单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PurchOrderDto purchOrder) {
        return toAjax(purchOrderService.updatePurchOrder(purchOrder));
    }

    /**
     * 删除采购订单
     */
    @RequiresPermissions("order:purchorder:remove")
    @Log(title = "采购订单", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(purchOrderService.deletePurchOrderByIds(ids));
    }


    /**
     * 删除采购订单
     */
    @RequiresPermissions("order:purchorder:audit")
    @Log(title = "采购订单", businessType = BusinessType.AUDIT)
    @PostMapping("/audit")
    @ResponseBody
    public AjaxResult audit(String ids) {
        return toAjax(purchOrderService.auditPurchOrderByIds(ids));
    }


    /**
     * 选择采购订单
     */
    @GetMapping("/selectPurchOrder")
    public String selectProductOrder() {
        return prefix + "/selectPurchOrder";
    }

    /**
     * 查询采购订单管理列表(选择页面)
     */
    @PostMapping("/selectList")
    @ResponseBody
    public TableDataInfo selectList(PurchOrder purchOrder) {
        startPage();
        List<PurchOrder> list = purchOrderService.selectPurchOrderList(purchOrder);
        return getDataTable(list);
    }

}
